// mongodb数据库基本操作封装
module.exports = {
  find: function(collection, params) {
    params ? true : params = {};
    // console.log(params.query)
    if (params.limit) {
      return collection.find(params.query || {}).sort(params.sort || {}).skip(parseInt(params.skip || 0)).limit(parseInt(params.limit)).toArray();
    } else {
      return collection.find(params.query || {}).sort(params.sort || {}).skip(parseInt(params.skip || 0)).toArray();
    };
  },
  findOne: function(collection, params) {
    console.log(params.query)
    return collection.findOne(params.query, params.option || {});
  },
  findOneByObjectId: function(collection, _id) {
    return collection.findOne({
      _id: new ObjectID(_id)
    });
  },
  update: function(collection, params) {
    return collection.updateMany(params.query, {
      $set: params.data
    }, {
      upsert: true,
      returnOriginal: false
    });
  },
  updateOne: function(collection, params) {
    return collection.updateOne(params.query, {
      $set: params.data
    }, {
      upsert: true,
      returnOriginal: false
    });
  },
  updateOneByObjectId: function(collection, params) {
    return collection.updateOne({
      _id: new ObjectID(params._id)
    }, {
      $set: params.data
    }, {
      upsert: true,
      returnOriginal: false
    });
  },
  findOneAndUpdate: function(collection, params) {
    return collection.findOneAndUpdate(params.query, {
      $set: params.data
    }, {
      upsert: true,
      returnOriginal: false
    });
  },
  findOneAndUpdateByObjectId: function(collection, params) {
    return collection.findOneAndUpdate({
      _id: new ObjectID(params._id)
    }, {
      $set: params.data
    }, {
      upsert: true,
      returnOriginal: false
    });
  },
  insert: function(collection, params) {
    return collection.inserMany(params.data, params.option || {});
  },
  insertOne: function(collection, params) {
    return collection.insertOne(params);
  },
  delete: function(collection, params) {
    return collection.deleteMany(params.query, params.option || {});
  },
  deleteOne: function(collection, params) {
    return collection.findOneAndDelete(params.query, params.option || {});
  },
  deleteOneByObjectId: function(collection, params) {
    return collection.findOneAndDelete({ _id: new ObjectID(params._id) }, params.option || {});
  },
  count: function(collection, params) {
    return collection.count(params || {});
  },
  drop: function(collection, params) {
    return collection.drop();
  },
  removeAll: function(collection) {
    return collection.removeMany();
  },
  remove: function(collection, params) {
    return collection.removeMany(params.query, params.option);
  },
  removeOne: function(collection, params) {
    return collection.removeOne(params.query, params.option || {});
  },
  removeOneByObjectId: function(collection, _id) {
    return collection.removeOne({
      _id: new ObjectID(_id)
    });
  },
  checkExists: function() {

  }
};
